
/*

DEPENDENCIES:
-------------
(none)
-------------


IN THIS FILE:
-------------
Gimme.Helper.index_of
Gimme.Helper.contains
Gimme.Helper.unique
Gimme.Helper.has_class
Gimme.Helper.get_attr
-------------

*/

(function()
{
	// CacheBit "class"	
	function CacheBit(name)
	{
		this.name = name;
		this.f = 0;
		this.t = 1;
		this.inc = function()
		{
			this.f = (this.t += 2) - 1;
		}		
	}
	
	var indexOf = function()
	{
		return typeof Array.prototype.indexOf !== 'undefined' ? native_indexOf : custom_indexOf;

		function native_indexOf(collection, item)
		{
			return collection.indexOf(item);
		}

		function custom_indexOf(collection, item)
		{
			var i, len = collection.length;
			for (i = 0; i < len; i++)
			{
				if (collection[i] === item)
				{
					return i;
				}
			}
			return -1;
		}
	}();
	
	function contains(collection, item)
	{
		return indexOf(collection, item) !== -1;
	}
	
	function hasClass(elem, classes)
	{
		var className = elem.className;
		if (typeof className !== 'string')
		{
			className = getAttr(elem, 'class');
		}
		
		if (!className)
		{
			return false;
		}

		var elemClass = ' ' + className + ' ';
		var i = classes.length;
		while (i--)
		{
			if (elemClass.indexOf(' ' + classes[i] + ' ') === -1)
			{
				return false;
			}
		}
		return true;
	}
	
	var getAttr = function()
	{
		// Hack: would prefer to avoid this check, but need some quick,
		// non-UAString way of avoiding using the native .getAttribute in IE
		return Gimme.Browser.is_ie ? ie : w3c;

		function w3c(_elem, _attr)
		{
			return _elem.getAttribute(_attr);
		}

		function ie(_elem, _attr)
		{
			switch (_attr.toLowerCase())
			{
				case 'class':
					return _elem.className || null;

				case 'id':
					return _elem.id || null;

				case 'href':
				case 'src':
					// IE Bug: when a '#default#userData' behavior is bound to an element, getAttribute fails to return anything useful.
					// Worse, it throws a run-time error if you try to invoke it with an iFlag parameter, as in: .getAttribute('href', 2);
					// Even more odd, typeof .getAttribute in this case returns "unknown" ( ?? what the heck! )
					// We can leverage this oddity, to avoid invoking .getAttribute(..) with an iFlag parameter.  In that case, we fall back
					// to trying to retrieve the attribute value through the element's .attributes collection.
					if (typeof _elem.getAttribute !== 'undefined')
					{
						// If typeof .getAttribute is not "unknown" we can safely use iFlag parameters when invoking .getAttribute(..).
						// It still might return null though, (in IE) so a series of logical ORs will help increase our chances of
						// retrieving the desired attribute value.
						return _elem.getAttribute(_attr, 2);
					}
					break;

				default:
					break;
			}

			return _elem.attributes && _elem.attributes[_attr] ? _elem.attributes[_attr].nodeValue : _elem.getAttribute(_attr);
		}
	}();

	
	var _uniqueBit;
	function unique(collection)
	{
		_uniqueBit.inc();
		var name = _uniqueBit.name;
		
		var uniqueCollection = [];
		var i, len = collection.length;
		for (i = 0; i < len; i++)
		{
			var elem = collection[i];
			if (elem[name] !== _uniqueBit.t)
			{
				elem[name] = _uniqueBit.t;
				uniqueCollection.push(elem);
			}
		}

		return uniqueCollection;
	}
	
	Gimme.Helper.index_of = indexOf;
	Gimme.Helper.contains = contains;	
	Gimme.Helper.has_class = hasClass;
	Gimme.Helper.get_attr = getAttr;
	Gimme.Helper.unique = unique;
	
})();